/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command.query;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.pilot.app.command.cmo.query.QueryDeptUserCmd;
import com.itgrail.pilot.app.command.co.UserDeptCO;
import com.itgrail.pilot.app.converter.UserClientConverter;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DepartmentMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DepartmentDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@CmdHandler
@Slf4j
public class QueryDeptUserCmdExe implements CommandExecutorI<ResultData<UserDeptCO>, QueryDeptUserCmd> {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepartmentMapper deptMapper;

    @Autowired
    private UserClientConverter userClientConverter;

    @Override
    public ResultData<UserDeptCO> execute(QueryDeptUserCmd queryDeptUserCmd) {
        String check = check(queryDeptUserCmd);
        if (check != null) {
            return ResultData.fail("400", check);
        }
        try {
            Integer deptId = queryDeptUserCmd.getDeptId();
            DepartmentDO deptDO = deptMapper.findByDeptId(deptId);
            if (deptDO == null) {
                return ResultData.fail("400", "部门不存在");
            }
            UserDeptCO userDeptCO = new UserDeptCO();
            userDeptCO.setDeptId(deptId);
            userDeptCO.setDeptName(deptDO.getName());

            List<UserDO> userDOList = userMapper.findByDeptId(deptId);
            userDeptCO.setUserList(userClientConverter.dataToClient(userDOList));

            List<UserDeptCO> childDeptList = Lists.newArrayList();
            List<DepartmentDO> deptList = deptMapper.findChildByDeptId(deptId);
            deptList.forEach(dept -> {
                childDeptList.add(userClientConverter.dataToClient(dept));
            });
            userDeptCO.setChildList(childDeptList);
            return ResultData.success(userDeptCO);
        } catch (Exception ex) {
            log.info("查询错误", ex);
            return ResultData.fail("查询错误");
        }
    }

    protected String check(QueryDeptUserCmd queryDeptUserCmd) {
        try {
            Preconditions.checkNotNull(queryDeptUserCmd, "queryDeptUserCmd不能为null");
            Preconditions.checkNotNull(queryDeptUserCmd.getDeptId(), "deptId不能为null");
        } catch (Exception ex) {
            log.info("查询参数错误:{}", ex.getMessage());
            return ex.getMessage();
        }
        return null;
    }
}
